package com.leenmvc.core.dao.sqlServer.wrapper;

import com.leenmvc.core.base.BaseEntity;
import com.leenmvc.core.utils.BeanUtils;
import com.leenmvc.core.utils.ClassUtil;
import com.leenmvc.core.utils.StringUtils;
import com.leenmvc.core.dao.communal.Wrapper;
import java.util.List;
import java.util.Map;

/**
 *
 * 拼装复杂sql语句的类
 *
 */
public class SqlServerCountWrapper extends Wrapper {

    public SqlServerCountWrapper() {}

    public SqlServerCountWrapper(Class<?> cls) {
        this.cls = cls;
        this.columns = ClassUtil.getColumns(cls);
    }

    @Override
    public SqlServerCountWrapper addSet(String setKey, Object value) {
        return null;
    }

    @Override
    public String getSql() {
        sql.setLength(0);
        sql.insert(0, "select count(1) from " + BaseEntity.getTableName(cls));
        if(where.length() > 0) {
            sql.append(" where " + where);
        }
        return sql.toString();
    }

    @Override
    public Wrapper and() {
        this.where.append(" and ");
        return this;
    }

    @Override
    public Wrapper joinField(String field) {
        return this;
    }

    @Override
    public Wrapper joinOne(String field) {
        return this;
    }

    @Override
    public Wrapper joinMul(String field) {
        return this;
    }

    public Wrapper and(String field, Object value) {
        if(value == null) return this;
        this.where.append(" and ").append(field + " = ?");
        paramList.add(value);
        conditons.add(BaseEntity.humpLine(field) + " = ?");
        return this;
    }

    // or 连接符
    public Wrapper or() {
        this.where.append(" or ");
        return this;
    }

    // 使用
    public Wrapper linkOr() {
        int index = this.where.lastIndexOf(conditons.get(conditons.size() - 1), this.where.length() - 1);
        this.where.insert(index, "(");
        this.where.append(" or)");
        return this;
    }

    // 如果传参数那么默认就是用等号连接
    public Wrapper or(String field, Object value) {

        this.where.append(" or ").append(field + " = ?");
        paramList.add(value);
        conditons.add(BaseEntity.humpLine(field) + " = ?");
        return this;
    }

    public SqlServerCountWrapper count(Class<?> cls) {
        this.cls = cls;
        this.sql.append("select count(1) from ").append(BaseEntity.getTableName(cls));
        return this;
    }

    // order by
    public SqlServerCountWrapper orderBy(String field, String desc) {
        this.orderBy.append(field).append(" ").append(desc);
        return this;
    }

    /**
     * 排除某个字段
     *
     * @param fields
     * @return
     */
    public SqlServerCountWrapper decField(String... fields) {
        return this;
    }

    /**
     * 只显示某些字段
     *
     * @param fields
     * @return
     */
    public SqlServerCountWrapper incField(String... fields) {
        return this;
    }

    /**
     * 限制条数
     *
     * @param pageInfo
     *
     * @return
     */
    public SqlServerCountWrapper limit(Integer... pageInfo) {
        return this;
    }

    /**
     * 等于
     */
    public SqlServerCountWrapper eq(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.eq(field, value);
        return this;
    }

    /**
     * 不等于
     *
     * @param field
     * @param value
     * @return
     */
    public SqlServerCountWrapper uEq(String field, Object value) {
        super.uEq(field, value);
        return this;
    }

    /**
     * 右边随意
     *
     * @param field
     * @param value
     * @return
     */
    public SqlServerCountWrapper likeR(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.likeR(field, value);
        return this;
    }

    /**
     * 左边随意
     *
     * @param field
     * @param value
     * @return
     */
    public SqlServerCountWrapper likeL(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.likeL(field, value);
        return this;
    }

    /**
     * 全like
     *
     * @param field
     * @param value
     * @return
     */
    public SqlServerCountWrapper like(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.like(field, value);
        return this;
    }

    /**
     * 根据数组中
     *
     * @param field
     * @param ins
     * @return
     */
    public SqlServerCountWrapper in(String field, Object[] ins) {
        if (ins == null) return this;
        super.in(field, ins);
        return this;
    }

    public <T> SqlServerCountWrapper in(String field, List<T> ins) {
        return this.in(field, ins.toArray());
    }

    // between
    public SqlServerCountWrapper between(String field, Object start, Object end) {
        setAnd();
        super.between(field, start, end);
        return this;
    }

    // <
    public SqlServerCountWrapper lt(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.lt(field, value);
        return this;
    }

    // >
    public SqlServerCountWrapper gt(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.gt(field, value);
        return this;
    }

    // >=
    public SqlServerCountWrapper ge(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.ge(field, value);
        return this;
    }

    // <=
    public SqlServerCountWrapper le(String field, Object value) {
        if (StringUtils.isEmpty(value)) return this;
        super.le(field, value);
        return this;
    }

    // is null
    public SqlServerCountWrapper isNull(String field) {
        setAnd();
        super.isNull(field);
        return this;
    }

    // is not null
    public SqlServerCountWrapper isNotNull(String field) {
        setAnd();
        super.isNotNull(field);
        return this;
    }

    /**
     * 将hashMap转成SqlServerCountWrapper
     * like关键字 规则如下（前缀）：
     * LL_  左像（%在左边）
     * RL_  右像 (%在右边)
     * AL_  全像 (%在两边)  AL_username   %leen%
     * orderBY关键字规则如下 （前缀）：
     * OB_  排序的字段  value值为DESC 或 ESC    OB_createTime   DESC
     * page, pageSize 特殊字保留字 （只做分页功能）
     *
     * @return
     */
    public SqlServerCountWrapper mapToWrapper(Map<String, Object> params) {
        super.mapToWrapper(params);
        return this;
    }
    @Override
    public void copy(Wrapper wrapper)  {
        this.where = BeanUtils.deepClone(wrapper.getWhere());
        this.paramList = BeanUtils.deepClone(wrapper.getParamList());
    }
}